﻿Public Partial Class frm_TankandCargo
    Inherits System.Web.UI.Page
    Dim conn As New System.Data.SqlClient.SqlConnection(connString)
    Dim clsGet As New ClassFunctionGetData
    Dim sum1 As Single


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim lbPage As Label = CType(Master.FindControl("lbPage"), Label)
            lbPage.Text = "TANK & CARGO STORAGE "
            ClearTextBoxes(Me)
            Dim dt As DataTable = clsGet.Get_Shore("", port.SelectedValue)
            If (dt.Rows.Count > 0) Then
                With ddlshore
                    .ClearSelection()
                    .DataTextField = "Shore_name"
                    .DataValueField = "Shore_Code"
                    .DataSource = dt
                    .DataBind()
                    .Items.Insert(0, "<--กรุณาเลือกสถานที่เก็บสินค้าสินค้า-->")
                    .Items(0).Value = "0"
                End With
            End If
        End If
    End Sub
    Private Sub gridTariff_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gridTariff.RowCommand
        Dim Index As Integer = Convert.ToInt32(e.CommandArgument)
        'If (gridItem.PageIndex <> 0) Then
        '    Index = Index Mod (gridItem.PageIndex * 10)
        'End If
        If e.CommandName = "cmddelete" Then
            Dim ds As DataSet = Session("MAT_USE")
            ds.Tables(0).Rows(Index).Delete()
            ds.Tables(0).AcceptChanges()
            Session("MAT_USE") = ds
            gridTariff.PageIndex = 0
            gridTariff.DataSource = Session("MAT_USE")
            gridTariff.DataBind()
        End If
    End Sub
    Private Sub LoadDataCountMAtUSe()
        Dim ds As DataSet = Session("MAT_USE")
        For i = 0 To gridTariff.Rows.Count - 1
            Dim txt As TextBox = CType(gridTariff.Rows(i).FindControl("txtCount"), TextBox)
            Dim chk As CheckBox = CType(gridTariff.Rows(i).FindControl("chk"), CheckBox)
            If (chk.Checked) Then
                ds.Tables(0).Rows(i).Item("MAT_ComputeM") = "Y"
            Else
                ds.Tables(0).Rows(i).Item("MAT_ComputeM") = "N"
            End If
            ds.Tables(0).Rows(i).Item("Count") = txt.Text
            ds.AcceptChanges()
        Next
        Session("MAT_USE") = ds
    End Sub

    Private Sub gridTariff_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridTariff.RowDataBound
        Dim pay, total As Single
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim chk As CheckBox = CType(e.Row.FindControl("chk_pay"), CheckBox)
            If chk.Checked = True Then
                pay += e.Row.DataItem("Count") * e.Row.DataItem("KBETR")
            End If
            sum1 += e.Row.DataItem("Count") * e.Row.DataItem("KBETR")

        End If

        total = sum1 - pay
        txtSum.Text = sum1.ToString("#,##0.00")
        txtTotal.Text = total.ToString("#,##0.00")


        'If e.Row.RowType = DataControlRowType.DataRow Then
        '    Dim txtCount As TextBox = CType(e.Row.FindControl("txtCount"), TextBox)
        '    Dim intSum As TextBox = CType(e.Row.FindControl("txtSum"), TextBox)

        '    Dim chk As CheckBox = CType(e.Row.FindControl("chk"), CheckBox)
        '    If (gridTariff.DataKeys(e.Row.RowIndex).Values("MAT_Relation") & "" = "Y") Then
        '        txtCount.Visible = False
        '        txtCount.Enabled = False
        '        e.Row.Cells(3).Text = "-"
        '        e.Row.Cells(4).Text = "-"
        '    End If
        '    If (gridTariff.DataKeys(e.Row.RowIndex).Values("MAT_ComputeM") & "" = "Y") Then
        '        chk.Checked = True
        '    Else
        '        chk.Checked = False
        '    End If
        'End If





    End Sub



    Private Sub btnLoad_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        gridTariff.PageIndex = 0
        gridTariff.DataSource = Session("MAT_USE")
        gridTariff.DataBind()
    End Sub

    Private Sub port_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles port.SelectedIndexChanged
        Dim dt As DataTable = clsGet.Get_Shore("", port.SelectedValue)
        If (dt.Rows.Count > 0) Then
            With ddlshore
                .ClearSelection()
                .DataTextField = "Shore_name"
                .DataValueField = "Shore_Code"
                .DataSource = dt
                .DataBind()
                .Items.Insert(0, "<--กรุณาเลือกสถานที่เก็บสินค้าสินค้า-->")
                .Items(0).Value = "0"
            End With
        End If
    End Sub

    'Private Sub BntSaveData_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BntSaveData.Click
    '    Dim comm As System.Data.SqlClient.SqlCommand
    '    Dim clsDB As New ClassDatabase
    '    Dim frm As New frm_OtherService

    '    Dim TG_ID As String = "TG-" & frm.GetMaxID("TankandCargo", "TG_ID")
    '    Dim cusid As Array

    '    cusid = TextBox1.Text.Split("-")

    '    Try
    '        conn.Open()
    '        Dim sql As String = ""
    '        sql = "INSERT INTO TankandCargo_Head (TG_ID,Customer_no,Day_Start,Day_End,SumTotal,CashType,Description) Values('" & TG_ID & "'," & _
    '        "'" & cusid(0) & "'," & _
    '        "'" & CDate(txtDateStart.Text).ToString("yyyyMMdd") & "'," & _
    '        "'" & CDate(txtDateEnd.Text).ToString("yyyyMMdd") & "'," & _
    '        "'" & txtSum.Text & "','0'," & _
    '        "'" & Description.Text & "')"

    '        comm = New System.Data.SqlClient.SqlCommand(sql, conn)
    '        comm.ExecuteNonQuery()

    '        For i As Integer = 0 To gridTariff.Rows.Count - 1
    '            Dim sumList As Double = 0
    '            Dim check As Char = ""
    '            Dim chk As CheckBox = CType(gridTariff.Rows(i).FindControl("chk_pay"), CheckBox)
    '            sumList = gridTariff.Rows(i).Cells(3).Text * gridTariff.Rows(i).Cells(6).Text
    '            If (chk.Checked = True) Then
    '                check = "1"
    '            Else
    '                check = "0"
    '            End If
    '            sql = "INSERT INTO TankandCargo (TG_ID,Material,Customer_no,Amount,Total,shore_Code) Values ('" & TG_ID & "','" & gridTariff.Rows(i).Cells(1).Text & "'," & _
    '            "'" & cusid(0) & "'," & _
    '            "'" & gridTariff.Rows(i).Cells(3).Text & "'," & _
    '            "'" & sumList & "'," & _
    '            "'" & ddlshore.SelectedValue & "')"
    '            comm = New System.Data.SqlClient.SqlCommand(sql, conn)
    '            comm.ExecuteNonQuery()
    '            sql = ""

    '        Next
    '        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('บันทึกข้อมูล สำเร็จ');", True)
    '        ClearTextBoxes(Me)

    '    Catch ex As Exception

    '        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert(' ไม่สามารถบันทึกข้อมูลได้');", True)

    '    End Try


    '    conn.Close()
    'End Sub
    Public Sub ClearTextBoxes(ByVal StartContainer As Control) 'ล้างข้อมูลใน Textbox ทั้งหมด
        Dim ctl As Control
        For Each ctl In StartContainer.Controls
            If (TypeOf ctl Is TextBox) Then
                DirectCast(ctl, TextBox).Text = ""
                If (TypeOf ctl Is DropDownList) Then
                    DirectCast(ctl, DropDownList).Items.Clear()
                End If
                If (TypeOf ctl Is GridView) Then
                    DirectCast(ctl, GridView).DataSource = Nothing
                    DirectCast(ctl, GridView).DataBind()

                End If
            Else
                If (ctl.Controls.Count > 0) Then ' ---- ถ้าคอนโทรลมีคอนโทรลย่อยภายใน
                    ClearTextBoxes(ctl) ' ---- recursive call - ให้เรียกตัวเอง เพื่อเคลียร์ TextBox ในนั้นต่อไป (ถ้ามี)
                End If
            End If
        Next
        Session("MAT_USE") = Nothing


    End Sub 'ลบค่าใน Textbox ทั้งหมด
  

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "M")
        End If
    End Sub
End Class